package com.uchengwang.ui.cm_ui.uu.http;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.cm_ui.uu.bean.ChatNoticeBean;
import com.uchengwang.ui.cm_ui.uu.bean.ChatNoticeListBean;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoBean;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;

/**
 * Created by U城网 on 2018/4/20.
 */

public class XiangliaoManager {

    private static XiangliaoManager manager;
    private XiangliaoManager(){}

    public static XiangliaoManager getManager() {
        if (manager == null){
            manager = new XiangliaoManager();
        }
        return manager;
    }

    /**
     * 发布一个享聊
     * */
    public void release(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.POST, "/chat", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }


    /**
     * 检查是否能进入享聊
     * */
    public void chatCheck(Activity activity, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.GET, "/chat/check ", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status=="+status+"==msg=="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status=="+status+"==msg=="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });

    }


    /**
     * 获取享聊对象
     * */
    public void getUsers(Activity activity, MapRequest map, final OnHttpResultListener<XiangliaoBean> listener){
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.GET, "/chat", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data)){
                    XiangliaoListBean bean = JsonUtils.getManager().stringToBean(data, XiangliaoListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 打招呼
     * */
    public void greet(Activity activity, long uid, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UID, uid);
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.POST, "/chat/greeting", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                listener.onResult(status, msg, 0, null);
            }
        });
    }

    /**
     * 获取我的享聊提醒
     * */
    public void getChatNotices(Activity activity, MapRequest map, final OnHttpResultListener<ChatNoticeBean> listener){
        HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.GET, "/chat/notice", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status== 0 && !TextUtils.isEmpty(data)){
                    ChatNoticeListBean bean = JsonUtils.getManager().stringToBean(data, ChatNoticeListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }


    /**
     * 根据心情类型 获取心情资源图片
     * */
    public int getEmotionResId(int emotionType){
        int resId = 0;
        switch (emotionType){
            case 1:
                resId = R.drawable.icon_1xiangliao_orange;
                break;
            case 2:
                resId = R.drawable.icon_2xiangliao_blue;
                break;
            case 3:
                resId = R.drawable.icon_3xiangliao_red;
                break;
            case 4:
                resId = R.drawable.icon_4xiangliao_green;
                break;
            case 5:
                resId = R.drawable.icon_5xiangliao_zise;
                break;
            case 6:
                resId = R.drawable.icon_6xiangliao_huise;
                break;
            case 7:
                resId = R.drawable.icon_7xiangliao_qingse;
                break;
        }
        return resId;
    }

    /**
     * 根据心情类型 获取对应的信息描述
     * */
    public String getEmotionTitle(int emotionType){
        String title = "";
        switch (emotionType){
            case 1:
                title = "橙色兴奋";
                break;
            case 2:
                title = "蓝色向往";
                break;
            case 3:
                title = "红色愤怒";
                break;
            case 4:
                title = "绿色感悟";
                break;
            case 5:
                title = "紫色压力";
                break;
            case 6:
                title = "灰色悲伤";
                break;
            case 7:
                title = "青色思念";
                break;
        }
        return title;
    }

}
